package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加图书");
        System.out.println("请输入你要添加的图书信息");
        Scanner in = new Scanner(System.in);
        //获取图书信息
        String name = in.nextLine();
        String author = in.nextLine();
        int price = in.nextInt();
        in.nextLine();
        String type = in.nextLine();
        Book book = new Book(name,author,price,type);

        //检查图书是否存在
        int currentSize = bookList.getUsedSize();
        for(int i = 0; i < currentSize; i++){
            Book book1 = bookList.getBooks(i);
            if(book1.getName().equals(name)){
                System.out.println("图书已存在");
                return;
            }
        }

        //开始添加
        bookList.setBooks(currentSize,book);
        System.out.println("添加成功");
        bookList.setUsedSize(currentSize + 1);
    }
}
